package com.algorithm;

import java.util.Arrays;

/**
 * @author: sun.hongliang
 * @create: 2019/11/25 16:20
 */
public class IntersectTest {

    public static void main(String[] args) {

    }

    public int[] intersect(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int num1Len = nums1.length;
        int num2Len = nums2.length;
        int p = 0, q = 0;
        int[] temp = new int[Math.max(num1Len, num2Len)];
        int i = 0;
        while (p < num1Len && q < num2Len) {
            if (nums1[p] == nums2[q]) {
                temp[i++] = nums1[p];
                p++;
                q++;
            } else if (nums1[p] > nums2[q]) {
                q++;
            } else {
                p++;
            }
        }
        int[] res = new int[i];
        System.arraycopy(temp, 0, res, 0, i);
        return res;
    }
}
